<?php

/**
 * Description of WebserviceManager
 *
 * @author bonfiglio
 * @todo Commenter !!!
 */
class WebserviceManager {

    //Définitions des groups (provider)
    protected $groups = array();
    
    //Définitions des webservices (provider)
    protected $webservices = array(
        'file-manager' => array(
            'sent-it' => array(
                'arguments' => array(
                    'path' => array(
                        'validate' => FILTER_SANITIZE_STRING,
                        'required' => 1
                    )
                ),
                'activate' => 1,
                'objectName' => 'WebserviceFileManager'
            )
        ),
        'precarite' => array(
            'get-waiting-contacts' => array(
                'arguments' => array(
                    'status' => array(
                        'validate' => FILTER_VALIDATE_INT,
                        'options' => array('default' => 0),
                    )
                ),
                'activate' => 1,
                'objectName' => 'WebservicePrecarite'
            ),
            'update-status' => array(
                'arguments' => array(
                    'token' => array(
                        'sanitize' => FILTER_SANITIZE_STRING,
                        'required' => 1
                    ),
                    'ids' => array(
                        'sanitize' => FILTER_SANITIZE_STRING,
                        'required' => 1
                    )
                ),
                'activate' => 1,
                'objectName' => 'WebservicePrecarite'
            )
        )
    );
    protected $arguments = array(
        'format-output' => array(
            'sanitize' => FILTER_SANITIZE_STRING,
            'options' => array('default' => 'json'),
            'values' => array('xml', 'json', 'pass')
        )
    );
    protected $webserviceGroup = '';
    protected $webserviceChild = '';

    public function __construct($webserviceGroup, $webserviceChild) {

        //Vérification si le webservice est défini et actif
        if ($this->checkAuthorisation($webserviceGroup, $webserviceChild)) {

            $this->webserviceGroup = $webserviceGroup;

            $this->webserviceChild = $webserviceChild;
        } else {
            throw new Exception("$webserviceGroup > $webserviceChild n'existe pas !");
        }
    }

    private function checkAuthorisation($webserviceGroup, $webservice) {
        return (is_string($webserviceGroup) && is_string($webservice) && isset($this->webservices[$webserviceGroup]) && isset($this->webservices[$webserviceGroup][$webservice]) && isset($this->webservices[$webserviceGroup][$webservice]['activate']) && $this->webservices[$webserviceGroup][$webservice]['activate'] == 1) ? $this->webservices[$webserviceGroup][$webservice]['activate'] : false;
    }

    private function getArguments() {
        return (isset($this->webservices[$this->webserviceGroup]) && isset($this->webservices[$this->webserviceGroup][$this->webserviceChild]) && isset($this->webservices[$this->webserviceGroup][$this->webserviceChild]['arguments'])) ? array_merge($this->arguments, $this->webservices[$this->webserviceGroup][$this->webserviceChild]['arguments']) : false;
    }

    public function getWebservice($post) {
        return new $this->webservices[$this->webserviceGroup][$this->webserviceChild]['objectName']($this->getOptions($post));
    }

    private function getOptions($post) {
        $return = WebserviceBase::$defaultOptions;

        $return['group'] = $this->webserviceGroup;

        $return['name'] = $this->webserviceChild;

        $arguments = $this->getArguments();

        if ($arguments) {
            foreach ($arguments as $field => $attributs) {
                $value = (isset($post[$field])) ? $post[$field] : null;
                $DataFilter = new DataFilter($value, $attributs);
                if (!$value) {
                    if ($DataFilter->isRequired()) {
                        $return['errors'][$field] = 'missing arguments !';
                    } elseif ($DataFilter->hasDefaultValue()) {
                        $return['post'][$field] = $DataFilter->getValue();
                    }
                } elseif ($DataFilter->isValid()) {
                    $return['post'][$field] = $value;
                }
            }
        }
        return $return;
    }

}

?>